#!/usr/bin/perl
#addprinter.cgi

require './printer-lib.pl';
&init_config();
&ReadParse();

&error_setup($text{'error'});
sub cm_get_drivers
{
	open(DR, "/sysvol/etc/plist");
	my @plist = <DR>;
	close(DR);
	my @drvlist=();
	my $i = 0;
	foreach (@plist){
		$_ =~ s/\r|\n//g;
		$_ =~ /(.*)=(\S+)/;
		$drvlist[$i]->{'product'} = $1;
		$drvlist[$i]->{'driver'} = $2;
		$i++;
	}
	return @drvlist;
}
sub cm_get_usb
{
	my ($prod,$manuf);
	open(UM, "cat /proc/bus/usb/devices|grep Manufacturer|");
	my @um = <UM>;
	close(UM);

        open(UP, "cat /proc/bus/usb/devices|grep Product|");
        my @up = <UP>;
        close(UP);

        open(UD, "cat /proc/bus/usb/devices|grep Driver|");
        my @ud = <UD>;
        close(UD);
	
	$i = -1;
	$j = -1;
	foreach (@ud) {
		$i++;
		if($_ =~ /usblp/) {
			$j = $i;
			last;
		}
	}
	if(($j > 0 ) || ($j = 0)) {
		$um[$j] =~ s/\r|\n//g;
		$um[$j] =~ /Manufacturer=(.*)/;
		$manuf = $1;
		$up[$j] =~ s/\r|\n//g;
		$up[$j] =~/Product=(.*)/;
		$prod =$1;
	}
	my @usbprint = ($manuf, $prod);
	return @usbprint;
}

my @usb = &cm_get_usb();

if(!$usb[0] && !$usb[1]) {
	#&error_setup("Error");
	&error($text{'err_nousb'});
}
&header($text{'set_title'}, "", "index", 0, 1,undef,undef);
#print "[$usb[0] | $usb[1]]";

print "<form name='addprint' action=actadd.cgi methiod=post onsubmit='on_form_submit()'>\n";
print "<table class=mode1 width=80%>\n";

print "<tr><td width=150>$text{'manuf'}</td><td>";
print $usb[0];
print "</td></tr>\n";

print "<tr><td width=150>$text{'product'}</td><td>";
print $usb[1];
print "</td></tr>\n";

print "<tr><td width=150>$text{'printname'}</td><td>";
print "<input type=text class=text name=pname>";
print "</td></tr>";

my @drivers = &cm_get_drivers();
print "<tr><td width=150>$text{'printdriver'}</td><td>";
#print "<select name=driver>";
#foreach(@drivers){
#	print "<option value='$_->{'driver'}'>$_->{'product'}</option>\n";
#}
#print "</select>";
print "<input type=text class=text name=driver>";
print "</td></tr>\n";


print "</table>\n";

print "<table border=0 class=mode1><tr><td>\n";
print "<input type=submit name=add value=$text{'submin'} class=button1 onclick='return on_create_click();'>\n";
print "</td></tr></table>\n";
print "<input type=hidden name=manu>";
print "<input type=hidden name=product>";
print "</form>\n";


&footer("index.cgi", $text{'index_title'});

